<?php

namespace App\Exports\Appraise;

use App\Models\Appraise\AppraiseModel;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromArray;

class AppraiseExport implements FromArray
{

    public Collection $originalData;
    public array      $excelHeaders;
    public array      $excelContents = [];

    public function __construct(Collection $originalData)
    {
        $this->originalData = $originalData;
    }

    public function array(): array
    {
        // TODO: Implement array() method.
        $this->generateExcelHeader();
        $this->originalData->each(
            function ($item) {
                array_push(
                    $this->excelContents, [
                    $item['createdAt'],
                    $item['shopCode'] . $item['shopName'],
                    AppraiseModel::TYPE_CN_MAP_TEXT[$item['type']] ?? "",
                    $item['tagText'],
                    $item['score'] . "星",
                    $item['desc'],
                    AppraiseModel::STATUS_MAP_TEXT[$item['status']] ?? "",
                    $item['dealTime'],
                    $item['manageName'],
                ]
                );
            }
        )->values()->toArray();
        return collect(
            [$this->excelHeaders]
        )->merge($this->excelContents)->toArray();
    }


    public function generateExcelHeader()
    {
        $this->excelHeaders = [
            '提交时间',
            '门店',
            '反馈类型',
            '问题标签',
            '评价星级',
            '反馈内容',
            '状态',
            '完成时间',
            '处理人',
        ];
    }
}
